package com.yonyou.cyxdms.sysAuth.service.service.manage.company;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyx.function.exception.DALException;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.swing.JLabel;

import com.yonyou.cyxdms.sysAuth.service.constants.CommonConstants;
import com.yonyou.cyxdms.sysAuth.service.constants.ManageDictCodeConstants;
import com.yonyou.cyxdms.sysAuth.service.dto.FileUploadInfoDTO;
import com.yonyou.cyxdms.sysAuth.service.entity.FileUploadInfoPO;
import com.yonyou.cyxdms.sysAuth.service.mapper.FileUploadInfoMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(
        rollbackFor = {Exception.class}
)
public class FileUploadInfoServiceImpl extends ServiceImpl<FileUploadInfoMapper, FileUploadInfoPO> implements FileUploadInfoService {
    private static final Logger logger = LoggerFactory.getLogger(com.yonyou.cyxdms.sysAuth.service.service.manage.company.FileUploadInfoServiceImpl.class);
    private static int interval = -5;
    @Resource
    FileUploadInfoMapper fileUploadInfoMapper;

    public FileUploadInfoServiceImpl() {
    }

    @Override
    @Transactional(
            readOnly = true
    )
    public IPage<Map<String, Object>> selectPageBysql(Page page, Map<String, Object> queryCondition) {
        page.setRecords(this.fileUploadInfoMapper.selectPageBySql(page, queryCondition));
        return page;
    }

    @Override
    @Transactional(
            readOnly = true
    )
    public List<Map<String, Object>> selectListBySql(Map<String, Object> queryCondition) {
        return this.fileUploadInfoMapper.selectListBySql(queryCondition);
    }



    @Override
    public Map<String, Object> insert(FileUploadInfoDTO fileUploadInfoDTO) {
        FileUploadInfoPO fileUploadInfoPO = (FileUploadInfoPO)fileUploadInfoDTO.transDtoToPo(FileUploadInfoPO.class);
        this.fileUploadInfoMapper.insert(fileUploadInfoPO);
        return ((FileUploadInfoPO)this.fileUploadInfoMapper.selectById(fileUploadInfoPO.getFileUploadInfoId())).toMaps();
    }

    @Override
    public Map<String, Object> update(Long id, FileUploadInfoDTO fileUploadInfoDTO) {
        FileUploadInfoPO fileUploadInfoPO = (FileUploadInfoPO)this.fileUploadInfoMapper.selectById(id);
        fileUploadInfoDTO.transDtoToPo(fileUploadInfoPO);
        this.fileUploadInfoMapper.updateById(fileUploadInfoPO);
        return fileUploadInfoPO.toMaps();
    }

    @Override
    public void deleteById(Long id) {
        int deleteCount = this.fileUploadInfoMapper.deleteById(id);
        logger.debug("删除数量：{}", deleteCount);
        if (deleteCount <= 0) {
            throw new DALException("删除失败,没有符合条件的数据");
        }
    }

    @Override
    public void deleteBatchIds(String ids) {
        List<Long> longList = StringUtils.convertStrToArray(ids, ",", Long.class);
        int deleteCount = this.fileUploadInfoMapper.deleteBatchIds(longList);
        logger.debug("删除数量：{}", deleteCount);
        if (deleteCount != longList.size()) {
            throw new DALException("删除失败,没有符合条件的数据");
        }
    }

    @Override
    public List<FileUploadInfoDTO> selectFileByCompanyId(Integer companyId,Integer billType) {
        return fileUploadInfoMapper.selectFileByCompanyId(companyId,billType);
    }

    @Override
    public List<Map<String, String>> selectUrlAndNameByCompanyId(Integer companyId, Integer billType) {
        List<FileUploadInfoPO> fileUploadInfoPos = fileUploadInfoMapper.selectList(new LambdaQueryWrapper<FileUploadInfoPO>()
                .eq(FileUploadInfoPO::getBillId, companyId)
                .eq(FileUploadInfoPO::getBillType, ManageDictCodeConstants.COMPANY_FILE));
        List<Map<String, String>> caseUrl = new ArrayList<>();
        for(FileUploadInfoPO po : fileUploadInfoPos){
            Map<String, String> url = new HashMap<>();
            url.put("url", po.getFileId());
            url.put("name", po.getFileName());
            caseUrl.add(url);
        }
        return caseUrl;
    }
    @Override
    public int insertFiles(Long billId, int billType, List<Map<String, String>> filesInfo) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        FileUploadInfoPO fileUploadInfoPO = new FileUploadInfoPO();
        LocalDateTime createdAt = LocalDateTime.now();
        if (filesInfo != null && filesInfo.size() != 0) {
            filesInfo.forEach(map ->{
                String fileName = map.get("name");
                fileUploadInfoPO.setAppId(loginInfoDto.getAppId());
                fileUploadInfoPO.setBillId( billId.intValue());
                fileUploadInfoPO.setBillType(billType);
                fileUploadInfoPO.setFileSize(0);
                fileUploadInfoPO.setFileId(map.get("url"));
                fileUploadInfoPO.setIsValid(10011001);
                fileUploadInfoPO.setFileName(fileName);
                fileUploadInfoPO.setIsDeleted(CommonConstants.IS_NO);
                fileUploadInfoPO.setRecordVersion(CommonConstants.RECORDVERSION);
                fileUploadInfoPO.setCreatedAt(createdAt);
                fileUploadInfoPO.setCreatedBy(loginInfoDto.getUserId().toString());
                fileUploadInfoPO.setUpdatedAt(createdAt);
                fileUploadInfoPO.setUpdatedBy(loginInfoDto.getUserId().toString());
                fileUploadInfoMapper.insert(fileUploadInfoPO);
            });
        } else {
            return 1;
        }
        return filesInfo.size();
    }
}
